package com.ifcar.app.view;

import com.ifcar.app.adapter.HeadlineAdapter;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 首页头条推荐组件.
 * 
 * @author wjying
 */
public class HeadlineGallery extends FrameLayout implements
		OnItemSelectedListener {
	private static final String TAG = "HeadlineGallery";

	/**
	 * Gallery
	 */
	private MyGallery mGallery;

	/**
	 * 指示器
	 */
	private Indicator mIndicator;

	private HeadlineAdapter mAdapter;

	private OnItemSelectedListener mOnItemSelectedListener;

	public HeadlineGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public HeadlineGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public HeadlineGallery(Context context) {
		super(context);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mGallery = (MyGallery) findViewById(R.id.gallery);
		mGallery.setAutoRun(0);
		mGallery.setOnItemSelectedListener(this);

		mIndicator = (Indicator) findViewById(R.id.headline_indicator);
	}

	public Gallery getGallery() {
		return mGallery;
	}

	public void setOnItemSelectedListener(OnItemSelectedListener l) {
		mOnItemSelectedListener = l;
	}

	public void setAdapter(HeadlineAdapter adapter) {
		mAdapter = adapter;
		mGallery.setAdapter(adapter);
		if (adapter != null) {
			mGallery.setSelection(mAdapter.getInitPosition(0));
		}
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		mGallery.setOnItemClickListener(listener);
	}

	@Override
	public void onItemSelected(AdapterView<?> adapter, View arg1, int position,
			long arg3) {
		final int count = mAdapter.getRealCount();
		if (mIndicator.getTotalItems() != count) {
			mIndicator.setTotalItems(count);
		}

		int realPosition = mAdapter.getRealPosition(position);
		mIndicator.setCurrentItem(realPosition);
		if (mOnItemSelectedListener != null) {
			mOnItemSelectedListener.onItemSelected(adapter, arg1, realPosition,
					arg3);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		if (mOnItemSelectedListener != null) {
			mOnItemSelectedListener.onNothingSelected(arg0);
		}
	}
	
	

	public void startAutoScroll(boolean autoScroll) {
		if (mGallery != null) {
			mGallery.startAutoScroll(autoScroll);
		}
	}

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if ((visibility != VISIBLE || !isShown()) && mGallery != null) {
            if (mAdapter != null && mAdapter.getCount() > 0) {
                mGallery.setSelection(mAdapter.getInitPosition(mGallery.getSelectedItemPosition()));
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mGallery != null && mAdapter != null && mAdapter.getCount() > 0) {
            mGallery.setSelection(mAdapter.getInitPosition(mGallery.getSelectedItemPosition()));
        }
    }
}
